(******************************************************************************
* DLL - API for virtual timers subsystem.                                     *
******************************************************************************)
library TimerMan;

uses
  Windows, Notify, NotifyDef, Timers;

{$R *.RES}

(*** Creating interval timer with object event handler ***)
function tmCreateIntervalTimer(
        hEventProc: TNotifierEvent;  // Client event handler
        Interval  : dword;    // Time interval, msec
        Mode      : byte;     // Timer mode
        Run       : boolean;  // Start timer immediately
        Msg,                  // Message code (2nd handler parameter)
        UserParam : dword     // User parameter (3rd handler parameter)
        ) : THandle;
  var T : TIntervalTimer;
  begin
    T := TIntervalTimer.Create(
        TCallEventNotify.Create(hEventProc, 0, Msg, UserParam),
        Mode, Run, Interval);
    Server.AddTimer(T);
    result := THANDLE(T);
  end;

(*** Creating interval timer ***)
function tmCreateIntervalTimerEx(
        hEventObj : THandle;  // Notify object handle
        Interval  : dword;    // Time interval, msec
        Mode      : byte;     // Timer mode
        Run       : boolean;  // Start timer immediately
        EventType : byte;     // Notify object type
        Msg,                  // Message code
        UserParam : dword     // User parameter for message
        ) : THandle;
  var N : TNotify; T : TIntervalTimer;
  begin
    N := MakeNotifier(hEventObj, 0, EventType, Msg, UserParam);
    if N = nil then Result := INVALID_HANDLE_VALUE
    else
      begin
        T := TIntervalTimer.Create(N, Mode, Run, Interval);
        Server.AddTimer(T);
        result := THANDLE(T);
      end;
  end;

(*** Closing timer ***)
procedure tmCloseTimer(hTimer : THandle);
  begin
    Server.DeleteTimer(TTimer(hTimer));
  end;

(*** Starting timer (enable work) ***)
procedure tmStartTimer(hTimer : THandle);
  begin
    TTimer(hTimer).Active := true;
  end;

(*** Stopping timer (disable work) ***)
procedure tmStopTimer(hTimer : THandle);
  begin
    TTimer(hTimer).Active := false;
  end;

(*** Resetting timer ***)
procedure tmResetTimer(hTimer : THandle);
  begin
    TTimer(hTimer).Reset;
  end;

(*** Set timer mode ***)
procedure tmSetTimerMode(hTimer : THandle; Mode : byte);
  begin
    TTimer(hTimer).Mode := Mode;
  end;

(*** Modify timer interval ***)
procedure tmSetTimerInterval(hTimer : THandle; Interval : dword);
  begin
    TIntervalTimer(hTimer).Interval := Interval;
  end;

(*** Creating synchronized period timer with object event handler ***)
function tmCreateFixedTimer(
        hEventProc: TNotifierEvent;  // Client event handler
        TimeMask  : ShortString;// Time period in CRON format
        Mode      : Byte;       // Timer mode
        Run       : Boolean;    // Start timer immediately
        Msg,                    // Message code
        UserParam : dword       // User parameter for message
        ) : THandle;
  var T : TFixedTimer;
  begin
    T := TFixedTimer.Create(
        TCallEventNotify.Create(hEventProc, 0, Msg, UserParam),
        Mode, Run, TimeMask);
    Server.AddTimer(T);
    result := THANDLE(T);
  end;

(*** Creating synchronized period timer ***)
function tmCreateFixedTimerEx(
        hEventObj : THandle;    // Notify object handle
        TimeMask  : ShortString;// Time period in CRON format
        Mode      : Byte;       // Timer mode
        Run       : Boolean;    // Start timer immediately
        EventType : Byte;       // Notify object type
        Msg,                    // Message code
        UserParam : dword       // User parameter for message
        ) : THandle;
  var N : TNotify; T : TFixedTimer;
  begin
    N := MakeNotifier(hEventObj, 0, EventType, Msg, UserParam);
    if (N = nil) then Result := INVALID_HANDLE_VALUE
    else
      begin
        T := TFixedTimer.Create(N, Mode, Run, TimeMask);
        Server.AddTimer(T);
        result := THANDLE(T);
      end;
  end;

(*** Modify fixed timer CRON mask ***)
procedure tmSetTimerMask(hTimer : THandle; TimeMask : shortstring);
  begin
    TFixedTimer(hTimer).TimeMask := TimeMask;
  end;

(*** Load fixed timer LastTime ***)
procedure tmSetLastTime(hTimer : THandle; var LastTime : TSystemTime);
  begin
    TFixedTimer(hTimer).LastTime := LastTime;
  end;

(*** Save fixed timer LastTime ***)
procedure tmGetLastTime(hTimer : THandle; var LastTime : TSystemTime);
  begin
    LastTime := TFixedTimer(hTimer).LastTime;
  end;

(*** Get fixed timer NextTime ***)
procedure tmGetNextTime(hTimer : THandle; var NextTime : TSystemTime);
  begin
    NextTime := TFixedTimer(hTimer).NextTime;
  end;

exports
  tmCreateIntervalTimer,
  tmCreateIntervalTimerEx,
  tmCloseTimer,
  tmStartTimer,
  tmStopTimer,
  tmResetTimer,
  tmSetTimerMode,
  tmSetTimerInterval,
  tmCreateFixedTimer,
  tmCreateFixedTimerEx,
  tmSetTimerMask,
  tmSetLastTime,
  tmGetLastTime,
  tmGetNextTime;

begin
end.
